package com.sheng.servlet.user;

import com.alibaba.fastjson.JSONArray;
import com.mysql.cj.util.StringUtils;
import com.sheng.pojo.User;
import com.sheng.service.user.UserServiceImpl;
import com.sheng.util.Constant;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        String method = req.getParameter("method");
        if (method.equals("savepwd") && method != null) {
            this.updatePwd(req, resp);
        } else if (method.equals("pwdmodify") && method != null) {
            this.pwdModify(req, resp);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    public boolean checkPwd(String oldPwd, String newPwd, String confirmPwd) {
        boolean flag = false;
        if (oldPwd.equals(newPwd)) {
            flag = true;
        }
        return flag;
    }

    public void updatePwd(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        User user = (User) req.getSession().getAttribute(Constant.USER_SESSION);
        String newPwd = req.getParameter("newpassword");
        if (user != null) {
            boolean flag = false;
            try {
                flag = new UserServiceImpl().updatePwd(user.getId(), newPwd);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (flag) {
                req.getSession().removeAttribute(Constant.USER_SESSION);
                resp.sendRedirect(req.getContextPath() + "/login.jsp");
            }
        }
        req.getRequestDispatcher("pwdmodify.jsp").forward(req, resp);
    }

    public void pwdModify(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Object o = req.getSession().getAttribute(Constant.USER_SESSION);
        String oldPassword = req.getParameter("oldpassword");

        Map<String, String> result = new HashMap<String, String>();
        if (o == null) {
            result.put("result", "session error");
        } else if (StringUtils.isNullOrEmpty(oldPassword)) {
            result.put("result", "error");
        } else {
            User user = (User) o;
            String userPassword = user.getUserPassword();
            if (oldPassword.equals(userPassword)) {
                result.put("result", "true");
            } else {
                result.put("result", "false");
            }
        }
        resp.setContentType("application/json");
        PrintWriter writer = resp.getWriter();
        writer.write(JSONArray.toJSONString(result));
        writer.flush();
        writer.close();
    }
}
